package com.yjhb.android.sbz.v2.activity

import android.content.Intent
import android.text.TextUtils
import com.yjhb.android.sbz.App.Companion.updateUUID
import com.yjhb.android.sbz.App.Companion.updateUserInfo
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.activity.BaseActivity
import com.yjhb.android.sbz.activity.MainActivity
import com.yjhb.android.sbz.entity.UserInfo
import com.yjhb.android.sbz.manager.im.IMManager
import com.yjhb.android.sbz.tool.ChannelUtil.getChannelNumber
import com.yjhb.android.sbz.tool.EventMsg
import com.yjhb.android.sbz.tool.InvitationCodeTool
import com.yjhb.android.sbz.tool.Tool
import com.yjhb.android.sbz.v2.mvp.presenter.LoginBindTelPresenter
import com.yjhb.android.sbz.v2.mvp.view.BindTelView
import com.jojo.android.zxlib.tool.Global
import com.jojo.android.zxlib.tool.ZxDataTool
import com.jojo.android.zxlib.tool.ZxDeviceTool
import com.jojo.android.zxlib.tool.ZxToast
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_bind_tel_v2.*
import org.greenrobot.eventbus.EventBus
import java.util.concurrent.TimeUnit


/**
 *  创建者 zx
 *  创建时间 2021/8/6 9:01
 */
class V2LoginBindTelActivity : BaseActivity<LoginBindTelPresenter>(), BindTelView {

    private var disposable: Disposable? = null
    private var openid:String? = null
    private var invitationCode: String? = null

    override fun initPresenter(): LoginBindTelPresenter {
        return LoginBindTelPresenter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_bind_tel_v2
    }

    override fun initUI() {
        openid = intent?.getStringExtra("openid")
    }

    override fun initData() {
        invitationCode = getChannelNumber(this)
    }

    override fun onResume() {
        super.onResume()
        //从剪切板获取邀请码
        Global.getInstance().mainHandler.postDelayed({
            if (TextUtils.isEmpty(invitationCode)) invitationCode = InvitationCodeTool.getInvitationCode()
        }, 200)
    }

    override fun regEvent() {
        back?.setOnClickListener {
            finish()
        }

        confirm_tv?.setOnClickListener {
            if (Tool.isEditEmpty(et_tel)) {
                ZxToast.showBottomToastLight("请输入手机号码")
                return@setOnClickListener
            }
            if (Tool.isEditEmpty(et_code)) {
                ZxToast.showBottomToastLight("请输入验证码")
                return@setOnClickListener
            }
            openid?.let {
                presenter.bindPhoneAndSyncData(it,et_tel.text.toString(), et_code.text.toString())
            }
        }

        getcode_tv?.setOnClickListener {
            if (Tool.isEditEmpty(et_tel)) {
                ZxToast.showBottomToastLight("请输入手机号码")
                return@setOnClickListener
            }
            if (!ZxDataTool.isMobile(et_tel.text.toString())) {
                ZxToast.showBottomToastLight("请输入正确的手机号")
                return@setOnClickListener
            }
            presenter.sendPhoneVerifyCode(et_tel.text.toString())
        }
    }


    override fun onFailure(code: Int, msg: String?) {
        loadingDialog?.cancel()
        ZxToast.showBottomToast(msg)
    }

    override fun onBindSuccess() {
        openid?.let {
            presenter?.newWxLogin(it,ZxDeviceTool.getAndroidId(this))
        }
    }

    override fun onWait() {
        loadingDialog?.show()
    }

    override fun onCodeSendSuccess() {
        loadingDialog?.cancel()
        offGetCode()
    }

    override fun onLoginSuccess(userInfo: UserInfo) {
        loadingDialog.cancel()
        updateUserInfo(userInfo)
        updateUUID(userInfo.uuid)
        IMManager.getInstance().login()
        EventBus.getDefault().post(EventMsg(EventMsg.LOGIN_SUCCESS, 1))
        if (userInfo.parentId == null && !TextUtils.isEmpty(invitationCode)) {
            EventBus.getDefault().post(EventMsg(EventMsg.REQUEST_BIND_INVITE_CODE, invitationCode))
        }
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    /**
     * 关闭获取验证码
     */
    private fun offGetCode() {
        getcode_tv.isEnabled = false
        Observable.intervalRange(0, 60, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<Long?> {
                    override fun onSubscribe(d: Disposable) {
                        disposable = d
                    }

                    override fun onNext(aLong: Long) {
                        getcode_tv?.text = "" + (60 - aLong) + "s后重新获取"
                    }

                    override fun onError(e: Throwable) {}
                    override fun onComplete() {
                        getcode_tv?.isEnabled = true
                        getcode_tv?.text = "获取验证码"
                    }
                })
    }
}